axios.defaults.baseURL = '../student/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest =function (data) {
    let transObj='';
    for(let i in data){
        transObj+=i+'='+data[i]+'&';
    }
    return transObj;
}
Vue.filter('cityFormat', function (city) {
    city = parseInt(city)
    switch (city) {
        case 1:
            return '北京'
        case 2:
            return '福州'
        case 3:
            return '厦门'
        case 4:
            return '龙岩'
        case 5:
            return '泉州'

    }
})
const vm = new Vue({
    el:'#dv',
    data:{
        showStudent: {
            size: 5,
            page: 1,
        },
        user:'',
        id: '',
        name: '',
        sex: 1,
        age: '',
        edu: '',
        city: 1,
        no: 0,
        upname:'',
        upsex: 1,
        upage: '',
        upedu: '',
        upcity: 1,
        upno: 0,
        flag: false,
        total: 0,
        list: []
    },
    methods:{
        getAllList(mes){
            axios.post('list.php',mes)
            //transformRequest就是用来处理转换的)
                .then(result => {
                    if(result.status===200){
                        let data = result.data.data.list
                        this.total = result.data.data.count
                        this.list=data
                    }else{
                        alert("服务器出错")
                    }

                })
        },
        add(){
            axios.post('add.php',
                {name:this.name,
                 sex:this.sex,
                 age:this.age,
                 edu:this.edu,
                 city:this.city,
                 no:this.no,
                })
                .then(result => {
                    if(result.status===200){
                        alert("添加成功")
                        this.getAllList(this.showStudent)
                        this.name = this.age = this.edu = this.no = ''
                    }else{
                        alert("服务器出错")
                    }

                })
        },
        del(id){
            axios.post('delete.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        alert("删除成功")
                        this.getAllList(this.showStudent)
                    } else {
                        alert('服务器出错')
                    }
                })
        },update(){
            axios.post('update.php',
                {id:this.id,
                 name:this.upname,
                 sex:this.upsex,
                 age:this.upage,
                 edu:this.upedu,
                 city:this.upcity,
                 no:this.upno,
                })
                .then(result => {
                    if(result.status===200){
                        this.flag = false
                        this.getAllList(this.showStudent)
                    }else{
                        alert("服务器出错")
                    }
                })
        },
        modify(id){
            this.flag = true
            this.id = id
            axios.post('get.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        let data = result.data.data
                        this.upname = data.name
                        this.upsex = data.sex
                        this.upage = data.age
                        this.upedu = data.edu
                        this.upcity = data.city
                        this.upno = data.no
                    } else {
                        alert("服务器出错")
                    }
                })
        },
        next(){
            this.showStudent.page += 1
            if (this.showStudent.page > Math.ceil(this.total / this.showStudent.size)) {
                return
            }
            this.getAllList(this.showStudent)
        },
        prev(){
            this.showStudent.page -= 1
            if (this.showStudent.page < 1) {
                return
            }
            this.getAllList(this.showStudent)
        },
        choosePage(index){
            this.showStudent.page = index
            this.getAllList(this.showStudent)
        },
        signout(){
            Cookies.remove('PHPSESSID')
            Cookies.remove('name')
            location.href='sign.html'
        }
    },
    created(){
        this.user = Cookies.get('name')
        this.getAllList(this.showStudent)
    }
})
